package com.itheima.exception;

import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 1.异常处理 实现HandlerExceptionResolver接口 同一处理所有异常,需要手动细分异常 返回类型被限定
 *
 * @author Chengfei
 * @date 2020/08/18
 */
//@Component
public class ExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        System.out.println("异常处理器正在执行中");

        ModelAndView mv = new ModelAndView();
        if (e instanceof NullPointerException){
            mv.addObject("msg","空指针异常");
        }else if (e instanceof AccessDeniedException){
            mv.addObject("msg","没有访问权限");

            System.out.println("111---222---333");
        }

        mv.setViewName("/error.html");
        return mv;
    }


}
